﻿using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace CsharpPoker.Core
{
    public class Mouse : DrawableGameComponent
    {
        // Mouse data ------------------------------------------------------ //

        // Spritebatch
        protected SpriteBatch spriteBatch = null;        
        
        public MouseState mouseState;
        public Point position = new Point();

        Texture2D mouseCursor;
        
        public Mouse(Game game, string textureLocation)
            : base(game)
        {
            spriteBatch = (SpriteBatch)Game.Services.GetService(typeof(SpriteBatch));
            mouseCursor = Game.Content.Load<Texture2D>(textureLocation);
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            mouseState = Microsoft.Xna.Framework.Input.Mouse.GetState();

            position.X = mouseState.X;
            position.Y = mouseState.Y;

            base.Update(gameTime);
        }

        /// <summary>
        /// Allows the game component to draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Draw(GameTime gameTime)
        {
            Rectangle destRect = new Rectangle(position.X, position.Y, 20, 20);

            //if (State == States.up)
            //{
            spriteBatch.Draw(mouseCursor, destRect, Color.White);
            //}

            base.Draw(gameTime);
        }

        // Button accessors ------------------------------------------------ //

        //public int Width
        //{
        //    get { return width; }
        //}

        ///// <summary>
        ///// Height of menu in pixels
        ///// </summary>
        //public int Height
        //{
        //    get { return height; }
        //}

        /// <summary>
        /// Position of component in screen
        /// </summary>
        //public Vector2 Position
        //{
        //    get { return position; }
        //    set { position = value; }
        //}

        //public Texture2D Texture
        //{
        //    set { buttonTexture = value; }
        //    get { return buttonTexture; }
        //}
    }

    /// <summary>
    /// This is a game component that implements a menu with text elements.
    /// </summary>
    public class Button : DrawableGameComponent
    {
        // Button data ----------------------------------------------------- //
        public enum States { hover, up, justReleased, down }

        // Spritebatch
        protected SpriteBatch spriteBatch = null;

        // Button Position
        Vector2 position = new Vector2(0, 0);

        // Items
        States state;

        //Textures
        Texture2D buttonTexture;

        Mouse mouse;
        MouseState prevMouseState;

        // Used for handle input
        //protected KeyboardState oldKeyboardState;

        // Size of button
        int width = 100;
        int height = 70;
        
        // For audio effects
        //protected AudioLibrary audio;

        // Button functionality -------------------------------------------- //

        /// <summary>
        /// Default constructor
        /// </summary>
        /// <param name="game">the main game object</param>
        /// <param name="normalFont">Font to regular items</param>
        /// <param name="selectedFont">Font to selected item</param>
        public Button(Game game, Mouse mouse, string textureLocation)
            : base(game)
        {
            //regularFont = normalFont;
            //this.selectedFont = selectedFont;
            //menuItems = new List<string>();
            
            // Get the current spritebatch
            spriteBatch = (SpriteBatch)Game.Services.GetService(typeof(SpriteBatch));

            buttonTexture = Game.Content.Load<Texture2D>(textureLocation);
            this.mouse = mouse;
            // Get the audio library
            //audio = (AudioLibrary)
            //    Game.Services.GetService(typeof(AudioLibrary));

            // Used for input handling
            //oldKeyboardState = Keyboard.GetState();
        }

        /// <summary>
        /// Width of menu in pixels
        /// </summary>


        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            if (!Visible)
                return;

            if ((mouse.mouseState.LeftButton == ButtonState.Released) &&
                 (prevMouseState.LeftButton == ButtonState.Pressed))
            {
                Rectangle buttonRect = new Rectangle((int)position.X, (int)position.Y, width, height);

                if (buttonRect.Contains(mouse.position))
                {
                    State = States.justReleased;
                }
            }
            else
            {
                State = States.up;
            }
            
            prevMouseState = mouse.mouseState;

            base.Update(gameTime);
        }

        /// <summary>
        /// Allows the game component to draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Draw(GameTime gameTime)
        {
            if (!Visible)
                return;

            Rectangle destRect = new Rectangle((int)position.X, (int)position.Y, width, height);

            //if (State == States.up)
            {
                spriteBatch.Draw(buttonTexture, destRect, Color.White);
            }
            
            base.Draw(gameTime);
        }

        // Button accessors ------------------------------------------------ //

        public States State
        {
            set { state = value; }
            get { return state; }
        }
        
        public Vector2 Position
        {
            get { return position; }
            set { position = value; }
        }

        public Texture2D Texture
        {
            set { buttonTexture = value; }
            get { return buttonTexture; }
        }

        public int Width
        {
            get { return width; }
        }
    }
}